package com.nosweattechnet.mapmyway;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import com.nosweattechnet.datatypes.Place;
import com.nosweattechnet.server.PlaceListAsyncTask;

import android.os.Bundle;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

//Displays the favorite Places of the current user
public class PlaceListActivity extends Activity {

	private String userid;
	private ArrayList<Place> places;
	private static final float titleFontSize=20;
	private static final float subTitleFontSize=12;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //Gets the current user using the application
        Intent intent=getIntent();
        userid=intent.getStringExtra(getString(R.string.user_id_intent));
        
        //Starts retrieving the information from the database
        PlaceListAsyncTask plat=new PlaceListAsyncTask(userid);
        plat.execute();
        
        //Sets the view to the one specified in the layout xml
        LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ScrollView parentView=(ScrollView) inflater.inflate(R.layout.activity_item_list,null);
        LinearLayout ll=(LinearLayout)parentView.getChildAt(0);
        
        //Gets the places from the server
        places=new ArrayList<Place>();
        try {
			places=plat.getPlaces();
		} 
        catch (InterruptedException e) {}
        catch (ExecutionException e) {}
        
        //Alerts the user if they have no favorite places
        if(places.size()<=0){
        	Toast.makeText(this, R.string.no_Place_Favorites,Toast.LENGTH_LONG).show();
        	finish();
        }
        
        //Adds the places to the list
        for(Place p:places){
        	View t=makeView(p);
        	ll.addView(t);
        }
        
        //sets the current view
        setContentView(parentView);
    }

    //Creates the entry in the list for a place
    private View makeView(final Place p){
    	RelativeLayout rl=new RelativeLayout(this);
    	
    	//Creates the title text and adds it to the overall entry
    	TextView ttitle=new TextView(this);
    	ttitle.setText(p.getName());
    	ttitle.setTextSize(PlaceListActivity.titleFontSize);
    	RelativeLayout.LayoutParams paramstitle=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    	paramstitle.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    	paramstitle.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    	rl.addView(ttitle,paramstitle);
    	ttitle.setId(1);
    	
    	//Creates the subtitle text and adds it to the overall entry
    	TextView tsubtitle=new TextView(this);
    	tsubtitle.setText(p.getDescription());
    	tsubtitle.setTextSize(PlaceListActivity.subTitleFontSize);
    	RelativeLayout.LayoutParams paramssubtitle=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    	paramssubtitle.addRule(RelativeLayout.BELOW,ttitle.getId());
    	paramssubtitle.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    	rl.addView(tsubtitle,paramssubtitle);
    	
    	//sets the functionality of when the user touches an entry
    	rl.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				DisplayPlaces.addPlace(p);
				finish();
			}
    	});
    	return rl;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_place_list, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        	//Allows the user to show all of their favorites at once
        	case R.id.place_list_show_all:
        		DisplayPlaces.addPlaces(places);
        		finish();
        		return true;
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
